package com.idol.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.idol.dubbo.service.IEmailService;
import com.idol.handler.BlockExceptionHandler;
import com.idol.service.ICodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author SupremeSir
 * @description
 * @className CodeController
 * @date 2021/2/15 23:23
 **/
@RestController
@RequestMapping("code")
@RefreshScope
public class CodeController {
    /**
     * 获取 Spring Cloud 统一配置中心的数据
     */
    @Value("${idol.message}")
    private String message;
    @Value("${mysql.url}")
    private String mysqlURL;

    @Autowired
    private ICodeService codeService;
    @Reference
    private IEmailService emailService;

    // 创建验证码并保存至数据库
    @RequestMapping("create/{email}")
    public boolean createCode(@PathVariable("email") String email) {
        String code = codeService.saveCode(email);
        return emailService.sendEmail(email, code);
    }

    // 消息总线功能验证
    @RequestMapping("config")
    // blockHandler 相关用于配置流控等，fallback
    @SentinelResource(value = "getConfig",blockHandlerClass = BlockExceptionHandler.class, blockHandler = "handleException",
            fallbackClass = BlockExceptionHandler.class,fallback = "handlerError")
    public String getConfig() {
        if (Math.random() > 0.5) {
            int i = 1 / 0;
        }
        return "Message ====>>> " + message + "  mysqlURL ====>>> " + mysqlURL;
    }

}
